package com.dhcc.DRGsHosp.application.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.PatientDrgApplication;
import com.dhcc.DRGsHosp.core.domain.IndexCal;
import com.dhcc.DRGsHosp.core.domain.PatientDrg;

@Named
@Transactional
public class PatientDrgApplicationImpl implements PatientDrgApplication,Serializable {
	private static final long serialVersionUID = 1L;
	private EntityRepository repository;
    
    private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}

	public PatientDrg getPatientDrg(String parid) {
		return PatientDrg.get(PatientDrg.class, parid);
	}
	
	public void creatPatientDrg(PatientDrg patientDrg) {
		patientDrg.save();
	}
	
	public void updatePatientDrg(PatientDrg patientDrg) {
		patientDrg .save();
	}
	
	public void removePatientDrg(PatientDrg patientDrg) {
		if(patientDrg != null){
			patientDrg.remove();
		}
	}
	
	public void removePatientDrgs(Set<PatientDrg> patientDrgs) {
		for (PatientDrg patientDrg : patientDrgs) {
			patientDrg.remove();
		}
	}
	
	public List<PatientDrg> findAllPatientDrg() {
		return PatientDrg.findAll(PatientDrg.class);
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.PatientDrgApplication#findPatientDRGsData(com.dhcc.DRGsHosp.core.domain.IndexCal)
	 */
	@Override
	public List<Map<String, Object>> findPatientDRGsData(
			IndexCal IndexCal) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>(); 
		String sql = "SELECT B .P00050000, P .PARDRG_CODE, P .VER_CODE, P .LOG_INFO, G .* "
				+ "FROM DRGS.MR_IPINFO M "
				+ "LEFT JOIN DRGS.MR_BASEINFO B ON M.PMAININDEX=B.PMAININDEX "
				+ "LEFT JOIN DRGS.T_PATIENTDRG P ON M .PMAININDEX = P .PARID "
				+ "LEFT JOIN DRGS.T_GROUPINGLOG G ON G .MR_ADM = M . PMAININDEX "
				+ "WHERE M .P00290000 BETWEEN "
				+ "TO_DATE ('"+IndexCal.getDateFrom()+"', 'YYYY-MM-DD') "
				+ "AND TO_DATE ('"+IndexCal.getDateTo()+"', 'YYYY-MM-DD')";
		List<Object[]> list = getRepository().createSqlQuery(sql).list();
		for (Object[] o : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("recordId", o[0]);
			map.put("drgCode", o[1]);
			map.put("verCode", o[2]);
			map.put("logInfo", o[3]);
			map.put("mr_id", o[4]);
			map.put("gl01", o[5]);
			map.put("gl02", o[6]);
			map.put("gl03", o[7]);
			map.put("gl04", o[8]);
			map.put("gl05", o[9]);
			map.put("gl06", o[10]);
			map.put("gl07", o[11]);
			map.put("gl08", o[12]);
			map.put("gl081", o[13]);
			map.put("gl09", o[14]);
			map.put("gl091", o[15]);
			map.put("gl101", o[16]);
			map.put("gl102", o[17]);
			map.put("gl103", o[18]);
			map.put("gl104", o[19]);
			map.put("gl105", o[21]);
			map.put("gl106", o[22]);
			map.put("gl11", o[23]);
			map.put("glOperCode", o[24]);
			map.put("glOperName", o[25]);
			
			resultList.add(map);
		}
		return resultList;
	}
	
}
